<template>
  <div class="container">
    <div class="top">
      <el-input
        style="width: 200px; margin-bottom: 5px"
        v-model="search.serial_code"
        placeholder="请输入订货单号"
        clearable
      ></el-input>
      <el-input
        style="width: 300px"
        :disabled="true"
        placeholder="请搜索供应商信息"
        v-model="supplierNameList.name"
      >
        <template slot="prepend">供应商</template>
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="supplierBtn"
        ></el-button>
      </el-input>
      <el-input
        style="width: 300px"
        :disabled="true"
        placeholder="请搜索仓库"
        v-model="warehouseList.name"
      >
        <template slot="prepend">仓库</template>
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="warehouseBtn"
        ></el-button>
      </el-input>

      <el-date-picker
        value-format="yyyy-MM-dd"
        v-model="date"
        type="daterange"
        :clearable="false"
        range-separator="至"
        start-placeholder="单据创建开始日期"
        end-placeholder="单据创建结束日期"
      ></el-date-picker>
      <el-date-picker
        value-format="yyyy-MM-dd"
        v-model="dates"
        type="daterange"
        :clearable="false"
        range-separator="至"
        start-placeholder="确认收货开始时间"
        end-placeholder="确认收货结束时间"
      ></el-date-picker>
      <el-select v-model="search.status" placeholder="收货状态">
        <el-option
          v-for="item in status"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-select v-model="search.is_avg" placeholder="入库状态">
        <el-option
          v-for="item in is_avgList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-button
        size="small"
        type="primary"
        class="handleSearch"
        icon="el-icon-search"
        @click="handleSearch()"
        >查询</el-button
      >
      <el-button
        size="small"
        type="danger"
        class="handleSearch"
        icon="el-icon-delete"
        @click="empty()"
        >清空</el-button
      >
      <el-button
        size="small"
        type="primary"
        icon="el-icon-circle-plus"
        @click="handleAddAdmin()"
        >新增商超订货单</el-button
      >
      <el-button
        type="info"
        size="small"
        icon="el-icon-tickets"
        @click="derive()"
        >导出</el-button
      >
    </div>
    <el-table
      max-height="600"
      :data="tableData"
      border
      style="width: 100%"
      empty-text="暂无数据"
    >
      <el-table-column
        prop="serial_code"
        label="编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="supplier_code"
        label="供应商编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="supplier_name"
        label="供应商名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="price"
        label="总价格"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="statusMsg"
        label="状态"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="create_at"
        label="创建时间"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="check_at"
        label="确认收货时间"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="dccode"
        label="配送中心编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="dcname"
        label="配送中心名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="whsecode"
        label="仓库编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="whsename"
        label="仓库名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="buyer_name"
        label="采购员名称"
        align="center"
      ></el-table-column>
      <el-table-column label="操作" width="300" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            v-if="scope.row.status == 1"
            icon="el-icon-edit"
            type="primary"
            @click="handleEdit(scope)"
            >编辑</el-button
          >
          <el-button
            v-if="scope.row.status == 1"
            size="small"
            type="success"
            class="handleSearch"
            icon="el-icon-edit"
            @click="audit(scope)"
            >确认收货</el-button
          >

          <el-button
            type="info"
            size="mini"
            icon="el-icon-info"
            @click="particular(scope)"
            >详情</el-button
          >
          <el-button
            v-if="scope.row.status == 1"
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      @page="handleSizeChange"
      @pagesize="handleCurrentChange"
      :pageData="pageData"
    ></pagination>
    <superIndentDialog
      :dialog="dialog"
      :dialogData="dialogData"
      @upData="getData"
    ></superIndentDialog>
    <superIndentDetailDialog
      :particularDialog="particularDialog"
    ></superIndentDetailDialog>
    <warehouseselect
      :warehouseselect="warehouseselect"
      @tag="warehouseTag"
    ></warehouseselect>
    <div style="height: 0">
      <supplierSearch
        :supplierdialog="supplierdialog"
        @tag="supplierTag"
      ></supplierSearch>
    </div>
  </div>
</template>

<script>
import { getToken } from "@/utils/auth";
import Pagination from "@/components/pagination";
import { getnowDate } from "@/utils/index";
import SupplierSearch from "@/components/superGrossMargin/supplierSearch";
import {
  selectPrepurchaseList,
  delPrepurchase,
  verifyPrepurchase,
} from "@/api/superIndent/superIndentList";
import superIndentDialog from "@/components/superIndent/superIndentDialog";
import SuperIndentDetailDialog from "@/components/superIndent/superIndentDetailDialog";
import Warehouseselect from "@/components/commodity/warehouseselect";
export default {
  components: {
    pagination: Pagination,
    SuperIndentDialog: superIndentDialog,
    superIndentDetailDialog: SuperIndentDetailDialog,
    supplierSearch: SupplierSearch,
    warehouseselect: Warehouseselect,
  },
  data() {
    return {
      //页面数据
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper",
      },
      search: {
        searchCode: "",
      },
      dialog: {
        centerDialogVisible: false,
        dialogType: "",
        id: "",
      },
      particularDialog: {
        centerDialogVisible: false,
        id: "",
      },
      dialogData: {
        buyer_id: "",
        buyer_name: "",
        buyer_code: "",
        supplier_code: "",
        supplier_id: "",
        supplier_name: "",
        dccode: "",
        dcname: "",
        is_avg: false,
        pre_time: "",
        send_people: "",
        driver_phone: "",
        car_number: "",
        receive_people: "",
        freight_fee: "",
        fee_way: false,
        warehouse_tube: "",
        quality_inspection: "",
        annex: "",
        imgUrl: "",
      },
      supplierNameList: {
        name: "",
        code: "",
      },
      warehouseList: {
        name: "",
        code: "",
      },
      //dialog信息
      supplierdialog: {
        centerDialogVisible: false,
      },
      warehouseselect: {
        centerDialogVisible: false,
      },
      date: [getnowDate().endtimer, getnowDate().endtimer],
      dates: [],
      search: {
        start_date: "",
        end_date: "",
        check_start_date: "",
        check_end_date: "",
        status: "",
        is_avg: "",
        serial_code: "",
      },
      status: [
        {
          value: -1,
          label: "全部",
        },
        {
          value: 1,
          label: "待收货",
        },
        {
          value: 2,
          label: "已收货",
        },
      ],
      is_avgList: [
        {
          value: -1,
          label: "全部",
        },
        {
          value: 0,
          label: "成本价入库",
        },
        {
          value: 1,
          label: "平均价入库",
        },
      ],
    };
  },

  created() {
    //页面数据
    this.getData();
  },
  methods: {
    //页面数据
    getData() {
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        supplier_code_str: this.supplierNameList.code,
        status: this.search.status == -1 ? "" : this.search.status,
        is_avg: this.search.is_avg == -1 ? "" : this.search.is_avg,
        start_date: this.search.start_date,
        end_date: this.search.end_date,
        check_start_date: this.search.check_start_date,
        check_end_date: this.search.check_end_date,
        serial_code: this.search.serial_code,
        warehouse_code: this.warehouseList.code,
      };
      selectPrepurchaseList(data)
        .then((res) => {
          if (res.code == 0) {
            res.data.paginate.data.forEach((result) => {
              result.statusMsg = result.status == 1 ? "待收货" : "已收货";
            });
            this.tableData = res.data.paginate.data;
            this.pageData.page = res.data.paginate.current_page;
            this.pageData.pagesize = res.data.paginate.per_page;
            this.pageData.total = res.data.paginate.total;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //新增
    handleAddAdmin() {
      this.dialog.centerDialogVisible = true;
      this.dialog.dialogType = "";
    },

    //超级供应商搜索
    supplierBtn() {
      this.supplierdialog = {
        centerDialogVisible: true,
      };
    },
    supplierTag(data) {
      console.log(data, 11111);
      this.supplierNameList = {
        name: "",
        code: "",
      };
      let arrName = [];
      let arrCode = [];
      data.forEach((res) => {
        arrName.push(res.name);
        arrCode.push(res.code);
      });
      this.supplierNameList.name = arrName.join(",");
      this.supplierNameList.code = arrCode.join(",");
    },
    //超级仓库搜索
    warehouseBtn() {
      this.warehouseselect = {
        centerDialogVisible: true,
      };
    },
    warehouseTag(data) {
      this.warehouseList = {
        name: "",
        code: "",
      };
      console.log(data, 11111);
      let arrName = [];
      let arrCode = [];
      data.forEach((res) => {
        arrName.push(res.name);
        arrCode.push(res.code);
      });
      this.warehouseList.name = arrName.join(",");
      this.warehouseList.code = arrCode.join(",");
    },
    //确认收货
    audit(scope) {
      let data = {
        id: scope.row.id,
      };
      this.$confirm("是否确认收货该条信息?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          verifyPrepurchase(data).then((res) => {
            if (res.code == 0) {
              this.getData();
              this.$message({
                type: "success",
                message: res.msg,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //编辑
    handleEdit(scope) {
      this.dialog.centerDialogVisible = true;
      this.dialog.dialogType = "edit";
      this.dialog.id = scope.row.id;
      this.dialogData = {
        buyer_id: scope.row.buyer_id,
        buyer_name: scope.row.buyer_name,
        buyer_code: scope.row.buyer_code,
        supplier_code: scope.row.supplier_code,
        supplier_id: scope.row.supplier_id,
        supplier_name: scope.row.supplier_name,
        dccode: scope.row.dccode,
        dcname: scope.row.dcname,
        is_avg: scope.row.is_avg == 1 ? true : false,
        pre_time: scope.row.pre_time,
        send_people: scope.row.send_people,
        driver_phone: scope.row.driver_phone,
        car_number: scope.row.car_number,
        receive_people: scope.row.receive_people,
        freight_fee: scope.row.freight_fee,
        fee_way: scope.row.fee_way == 2 ? true : false,
        warehouse_tube: scope.row.warehouse_tube,
        quality_inspection: scope.row.quality_inspection,
        annex: scope.row.annex,
        imgUrl: this.imgUrl + scope.row.annex,
      };
    },

    // 本地搜索
    handleSearch() {
      if (this.date.length > 0) {
        this.search.start_date = this.date[0];
        this.search.end_date = this.date[1];
      }
      if (this.dates.length > 0) {
        this.search.check_start_date = this.dates[0];
        this.search.check_end_date = this.dates[1];
      }
      this.pageData.page = 1;
      this.pageData.total = null;
      this.getData();
    },
    //删除
    handleDelete(scope) {
      let data = {
        id: scope.row.id,
      };
      this.$confirm("是否删除该条信息?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delPrepurchase(data).then((res) => {
            if (res.code == 0) {
              this.getData();
              this.$message({
                type: "success",
                message: res.msg,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //导出
    derive() {
      this.$confirm("是否导出?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let link = document.createElement("a");

          if (this.date.length > 0) {
            this.search.start_date = this.date[0];
            this.search.end_date = this.date[1];
          }
          if (this.dates.length > 0) {
            this.search.check_start_date = this.dates[0];
            this.search.check_end_date = this.dates[1];
          }
          let status = this.search.status == -1 ? "" : this.search.status;
          link.href =
            this.$axios.defaults.baseURL +
            "/smkapi/Prepurchase/exportPrepurchaseData?token=" +
            getToken() +
            "&supplier_code_str=" +
            this.supplierNameList.code +
            "&status=" +
            status +
            "&is_avg=" +
            this.search.is_avg +
            "&start_date=" +
            this.search.start_date +
            "&end_date=" +
            this.search.end_date +
            "&check_start_date=" +
            this.search.check_start_date +
            "&check_end_date=" +
            this.search.check_end_date +
            "&serial_code=" +
            this.search.serial_code +
            "&warehouse_code=" +
            this.warehouseList.code;
          link.click();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消导出",
          });
        });
    },
    empty() {
      this.date = [getnowDate().endtimer, getnowDate().endtimer];
      this.dates = [];
      this.search = {
        start_date: "",
        end_date: "",
        check_start_date: "",
        check_end_date: "",
        status: "",
        is_avg: "",
        serial_code: "",
      };
      this.supplierNameList = {
        name: "",
        code: "",
      };
      this.warehouseList.code = "";
      this.warehouseList.name = "";
    },
    //详情
    particular(scope) {
      this.particularDialog.centerDialogVisible = true;
      this.particularDialog.id = scope.row.id;
    },
    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;

      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;

      this.getData();
    },
    //选取
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
  },
};
</script>
<style scoped>
.top {
  position: relative;
}

.add {
  position: absolute;
  right: 0;
  top: 4px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>
